package com.sports.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sports.dto.ScoreADTO;
import com.sports.dto.ScoreIDTO;
import com.sports.entity.Athletes;
import com.sports.entity.Item;
import com.sports.entity.Score;
import com.sports.service.AthletesService;
import com.sports.service.ItemService;
import com.sports.service.ScoreService;
import com.sports.mapper.ScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LZL
 * @description 针对表【t_score】的数据库操作Service实现
 * @createDate 2024-03-21 20:55:04
 */
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score>
        implements ScoreService {

    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private AthletesService athletesService;
    @Autowired
    private ItemService itemService;

    @Override
    public List<ScoreADTO> showItemNameById(String athletesName) {
        QueryWrapper<Athletes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("athletes_name",athletesName);
        List<Athletes> list = athletesService.list(queryWrapper);
//        System.err.println("list = " + list);
//        list.forEach(System.err::println);
        if (ObjUtil.isEmpty(list)){
            return null;
        }
        List<Integer> ids = athletesService.list(new LambdaQueryWrapper<Athletes>().eq(Athletes::getAthletesName, athletesName)).stream().map(Athletes::getAthletesId).collect(Collectors.toList());
        List<ScoreADTO> scoreADTOS = scoreMapper.selectItemNameById(ids);
        scoreMapper.selectItemNameById(ids).forEach(System.out::println);
        return scoreADTOS;
    }

    @Override
    public Page<ScoreIDTO> showAthletesNameById(Integer pageNum, Integer pageSize, String ItemName) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Item_name",ItemName);
        Item one = itemService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(one)){
            return null;
        }
        Integer itemId = one.getItemId();

        return scoreMapper.selectAthletesNameById(new Page(pageNum, pageSize), itemId);
    }
}




